package org.scalpel.mainApps;


import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import android.content.Context;
import android.graphics.*;
import android.view.*;
import android.util.AttributeSet;
import android.util.Log;
import android.content.res.Resources;

import org.scalpel.mainApps.R;
import org.scalpel.HHObjects.GUIDispatcher;
import org.scalpel.HHObjects.HHObjectInspector;
import org.scalpel.HHObjects.Loader;
import org.scalpel.common.HHObject;
import org.scalpel.common.RPCServer;
import org.scalpel.common.XMLHandler;

/**
 * 
 * @author Sylvere Billout
 * @version 1.0
 * 
 * Instantiate robot controller and all needed modules
 *    
 *
 */
public class RobotView extends View{

  private Loader fLoader = null;
  private GUIDispatcher fGuiDispatcher = null;

  private Paint textPaint;
  private Paint fRobotPaint;
  
  RPCServer fRpcServer = null;
  Thread fRpcThread = null;
  
  HashMap<String, Object> fListObject;

  public boolean onTouch(MotionEvent event){
    if( fGuiDispatcher != null )
      fGuiDispatcher.onTouch(event);
    return true;
  }
  
  protected void initView() {
    
    setFocusable(true);
    
    Resources r = this.getResources();

    fRobotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    fRobotPaint.setColor(r.getColor(R.color.background_color));
    fRobotPaint.setStrokeWidth(1);
    fRobotPaint.setStyle(Paint.Style.FILL_AND_STROKE);

    textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setColor(r.getColor(R.color.text_color));
    

    try {
      
      /** Handling XML */
      SAXParserFactory spf = SAXParserFactory.newInstance();
      SAXParser sp = spf.newSAXParser();

      Resources res = getResources();
      InputStream xip = res.openRawResource(R.raw.worldmap);

      /** Create handler to handle XML Tags ( extends DefaultHandler ) */
      XMLHandler myXMLHandler = new XMLHandler(getContext());
      sp.parse(xip, myXMLHandler);
      
      fListObject = myXMLHandler.getAllObject();
      
      fRpcServer = new RPCServer(fListObject);
      
      fRpcThread = new Thread(new Runnable() {
        public void run() {
          fRpcServer.listen();
        }
      });
      fRpcThread.start();
      
      ((HHObjectInspector)fListObject.get("world.inspector")).setObjectList(fListObject);
      
      fGuiDispatcher = ((GUIDispatcher)fListObject.get("world.guidispatcher"));
      if( fGuiDispatcher != null )
        fGuiDispatcher.initView(getContext());
      
      fLoader = ((Loader)fListObject.get("world.loader"));
      if( fLoader != null )
        fLoader.onLoad();
      
    } catch (Exception e) {
      Log.e("RobotView", "XML Pasing Excpetion = " + e.toString());
    }
  }
  
  @Override
  protected void onDraw(Canvas canvas){
    if( fGuiDispatcher != null )
      fGuiDispatcher.onDraw(canvas, fRobotPaint);

  }

  public RobotView(Context context) {
    super(context);
    initView();
  }

  public RobotView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView();
  }

  public RobotView(Context context, 
                     AttributeSet ats, 
                     int defaultStyle) {
    super(context, ats, defaultStyle);
    initView();
  }

  public void finish() {
    fGuiDispatcher.finish();
    if(fRpcThread != null){
      Thread moribund = fRpcThread;
      fRpcThread = null;
      moribund.interrupt();
      try {
        moribund.join(500);
      } catch (InterruptedException e) {
      }
    }
    if( fRpcServer != null)
    	fRpcServer.finish();

    Set<String> cles = fListObject.keySet();
    Iterator<String> it = cles.iterator();
    while (it.hasNext()){
       Object cle = it.next();
       HHObject valeur = (HHObject)fListObject.get(cle);
       valeur.finish();
    }
    fListObject.clear();
  }
}
